{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11470\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "market = \"hair_dryer\"\n",
    "# market = \"microwave\"\n",
    "# market = \"pacifier\"\n",
    "\n",
    "inputexcel = pd.read_excel(\"../Problem_C_Data/\" + market + '.xlsx', market)\n",
    "# 评论的个数\n",
    "num_review = len(list(inputexcel['star_rating']))\n",
    "print(num_review)\n",
    "\n",
    "xlsxxlsx = '../Problem_C_Data/good_contain.xls'\n",
    "good_words = pd.read_excel(xlsxxlsx, 'Sheet1')\n",
    "good_words = good_words.key.values.tolist()\n",
    "xlsxxlsx = '../Problem_C_Data/bad_contain.xls'\n",
    "bad_words = pd.read_excel(xlsxxlsx, 'Sheet1')\n",
    "bad_words = bad_words.key.values.tolist()\n",
    "# print(bad_words[1:5])\n",
    "# good_words[3]\n",
    "\n",
    "\n",
    "writer = pd.ExcelWriter(\"./2e_output/apriori star rating descriptors 2e akapriori select first contain \"+market+\".xlsx\",)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "import re\n",
    "def load_selected_star(inputexcel,star_rating_12345):\n",
    "    # 一行代表一个评论，一行第一个元素是star_rating\n",
    "    output = []\n",
    "    star_rating_list = list(inputexcel[inputexcel['star_rating']==star_rating_12345]['star_rating'])\n",
    "    review_headline_list = list(inputexcel[inputexcel['star_rating']==star_rating_12345]['review_headline'])\n",
    "    review_body_list = list(inputexcel[inputexcel['star_rating']==star_rating_12345]['review_body'])\n",
    "\n",
    "    for ii in range(len(star_rating_list)):\n",
    "    #     用正则表达式替换掉所有不是英文字母和空格的字符变成空格\n",
    "        review_headline = re.sub('[^a-zA-Z\\s]', ' ', str(review_headline_list[ii]))\n",
    "        review_body = re.sub('[^a-zA-Z\\s]', ' ', str(review_body_list[ii]))\n",
    "    #     避免review_headline最后一个单词和review_body第一个单词挨在一起，中间加个空格\n",
    "        review = review_headline + ' ' + review_body\n",
    "    #     小写 以空格分词\n",
    "        reviews = review.lower().split(' ')\n",
    "        onereview = []\n",
    "    #     提取出评论文本中的每个单词\n",
    "        for review_word in reviews:\n",
    "    #         如果在好词列表中，那么就加进该评论对应的的词表\n",
    "            if review_word in good_words:\n",
    "                onereview.append(review_word)\n",
    "    #         如果在坏词列表中，那么就加进该评论对应的的词表\n",
    "            if review_word in bad_words:\n",
    "                onereview.append(review_word)\n",
    "    #     将该评论的词表添加到所有评论词表末尾\n",
    "        output.append(list(str(star_rating_list[ii]))+onereview)\n",
    "    return output"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          frozensetA frozensetB   support  confidence      lift\n",
      "0         (money, 1)    (waste)  0.092054    0.572289  5.418371\n",
      "1         (1, waste)    (money)  0.092054    0.871560  5.418371\n",
      "2       (stopped, 1)  (working)  0.087209    0.796460  5.407545\n",
      "3       (working, 1)  (stopped)  0.087209    0.592105  5.407545\n",
      "4  (disappointed, 1)     (very)  0.052326    0.568421  2.464750\n",
      "\n",
      "     frozensetA frozensetB   support  confidence      lift\n",
      "0  (stopped, 2)  (working)  0.070423    0.737705  6.639344\n",
      "1  (working, 2)  (stopped)  0.070423    0.633803  6.639344\n",
      "2  (working, 2)   (months)  0.059468    0.535211  2.533333\n",
      "3   (2, worked)    (great)  0.051643    0.417722  2.494617\n",
      "4    (great, 2)   (worked)  0.051643    0.308411  2.494617\n",
      "\n",
      "   frozensetA frozensetB   support  confidence      lift\n",
      "0  (works, 3)     (well)  0.066066    0.402439  2.284299\n",
      "1   (well, 3)    (works)  0.066066    0.375000  2.284299\n",
      "2     (3, if)      (use)  0.075075    0.398936  1.916044\n",
      "3    (use, 3)       (if)  0.075075    0.360577  1.916044\n",
      "4   (last, 3)      (one)  0.051051    0.515152  1.913146\n",
      "\n",
      "    frozensetA frozensetB   support  confidence      lift\n",
      "0    (well, 4)    (works)  0.107347    0.578406  2.349495\n",
      "1   (works, 4)     (well)  0.107347    0.436047  2.349495\n",
      "2  (really, 4)     (like)  0.061546    0.465704  1.891697\n",
      "3      (if, 4)      (use)  0.060592    0.370262  1.713179\n",
      "4     (dry, 4)      (use)  0.054866    0.359375  1.662804\n",
      "\n",
      "   frozensetA frozensetB   support  confidence      lift\n",
      "0   (ever, 5)     (best)  0.058920    0.765504  6.579408\n",
      "1   (best, 5)     (ever)  0.058920    0.506410  6.579408\n",
      "2   (well, 5)    (works)  0.055489    0.430556  2.456548\n",
      "3  (works, 5)     (well)  0.055489    0.316596  2.456548\n",
      "4   (like, 5)      (one)  0.064290    0.403181  1.588086\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 选取出所有 star_rating_12345 的评论\n",
    "from akapriori import apriori\n",
    "\n",
    "# for star_rating_12345 in range(1, 6):\n",
    "star_rating_12345 = 1\n",
    "output = load_selected_star(inputexcel, star_rating_12345)\n",
    "\n",
    "# print(len(output))\n",
    "# for r in output[1:6]:\n",
    "#         print(r)\n",
    "# print()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "https://github.com/aknd/akapriori\n",
    "https://blog.csdn.net/qq_35515661/article/details/87391328\n",
    "\n",
    "transactions 待处理数据 列表套多元组的格式 [(),()...]\n",
    "\n",
    "support 最小支持度 P(AB) \n",
    "Support(A→B)= P(A∩B) \n",
    "\n",
    "confidence 最小置信度  P(B/A)  条件概率  \n",
    "P(AB)/P(A)\n",
    "Confidence(A→B)=P(B|A)=P(A∩B)/P(A)\n",
    "\n",
    "lift 判断的阈值 1/P(A)  先验概率的倒数  \n",
    "P(AB)/(P(A)P(B)) Lift=1时表示A和B独立。\n",
    "Lift(A→B)=Confidence(A→B)/Support(B)=P(B|A)/P(B)\n",
    "\n",
    "minlen maxlen  候选集最小长度 最大长度\n",
    "\n",
    "'''\n",
    "\n",
    "rules = apriori(output, support=0.05, confidence=0.3,\n",
    "                lift=0, minlen=0, maxlen=5)\n",
    "# 根据参数进行排序输出 排序优先级： lift 降序, confidence 降序, support 降序\n",
    "rules_sorted = sorted(rules, key=lambda x: (\n",
    "    x[4], x[3], x[2]), reverse=True)\n",
    "'''\n",
    "rules_sorted[0]  frozenset\n",
    "rules_sorted[1]  frozenset\n",
    "rules_sorted[2]  support 最小支持度 P(AB)\n",
    "rules_sorted[3]  confidence 最小置信度  P(B/A)  条件概率\n",
    "rules_sorted[4]  lift 判断的阈值 1/P(A)  先验概率的倒数\n",
    "'''\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查找所有 含有 star_rating_12345 的 rules_sorted\n",
    "rules_sorted_contain_star_rating_12345 = []\n",
    "for rules_sort in rules_sorted:\n",
    "    if (str(star_rating_12345) in rules_sort[0]) or (str(star_rating_12345) in rules_sort[1]):\n",
    "        rules_sorted_contain_star_rating_12345.append(rules_sort)\n",
    "\n",
    "# list → dataframe\n",
    "rules_sorted_contain_star_rating_12345 = pd.DataFrame(\n",
    "    rules_sorted_contain_star_rating_12345, columns=[\n",
    "        'frozensetA', 'frozensetB', 'support', 'confidence', 'lift'])\n",
    "\n",
    "print(rules_sorted_contain_star_rating_12345.head())\n",
    "print()\n",
    "\n",
    "rules_sorted_contain_star_rating_12345.to_excel(\n",
    "    writer, sheet_name=str(star_rating_12345), index=False)\n",
    "\n",
    "\n",
    "writer.save()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "code_folding": []
   },
   "outputs": [],
   "source": [
    "# # 最多打印这么多\n",
    "# printnummax = 10\n",
    "# # 输出 rules_sorted\n",
    "# if len(rules_sorted) > printnummax:\n",
    "#     for r in rules_sorted[0:printnummax]:\n",
    "#         print(r)\n",
    "# else:\n",
    "#     for r in rules_sorted:\n",
    "#         print(r)\n",
    "\n",
    "# print()\n",
    "\n",
    "\n",
    "# # list → dataframe\n",
    "# df = pd.DataFrame(rules_sorted, columns=[\n",
    "#                   'frozensetA', 'frozensetB', 'support', 'confidence', 'lift'])\n",
    "# df.to_excel(\"apriori_star_rating_descriptors_2e_akapriori_select_first.xlsx\",\n",
    "#             sheet_name=str(star_rating_12345), index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 最多打印这么多\n",
    "# printnummax  = 10\n",
    "# # 输出 rules_sorted_contain_star_rating_12345\n",
    "# if len(rules_sorted_contain_star_rating_12345) > printnummax:\n",
    "#     for r in rules_sorted_contain_star_rating_12345[0:printnummax]:\n",
    "#         print(r)\n",
    "# else:\n",
    "#     for r in rules_sorted_contain_star_rating_12345:\n",
    "#         print(r)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
